﻿<%@ WebHandler Language="C#" Class="FamilyHandlerService" %>

using System;
using System.Web;
using System.Web.SessionState;


using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.Page;


public class FamilyHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName){}


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

    //修改家庭信息
    public string modify_One_Family_Info()
    {
        string sname = HttpContext.Current.Request.Form["StudentName"];
        string sex = HttpContext.Current.Request.Form["StudentSex"];
        string fname = HttpContext.Current.Request.Form["FatherName"];
        string mobile = HttpContext.Current.Request.Form["Mobile"];

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int ReturnValue = 0;

        bool b = XJX.Page.Family.modify_One_Family_Info(UID,sname,sex,fname,mobile, out ReturnValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                HttpContext.Current.Session["FamilyInfo"]=null;//清除身份保存标识，重新登录系统！

                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "家庭信息修改成功，请重新登录！").ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "家庭信息修改失败，请检查数据!").ToJson();
                break;
        }
        return result;
    }
    
    //获取家庭信息
    public string Get_One_Family_Info()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        return XJX.Page.Family.Get_One_Family_Info_From_UID(UID, "");//第二个参数没有实质性意义，只为区分调用方法。
    }
}